package com.xxy.controller;

import com.xxy.entity.Category;
import com.xxy.entity.dto.CategoryQueryDTO;
import com.xxy.result.PageResult;
import com.xxy.result.Result;
import com.xxy.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 新增商品
     * @param category
     * @return
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success();

    }

    /**
     * 修改
     * @param category
     * @return
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteOne(@PathVariable Integer id) {
        categoryService.removeById(id);
        return Result.success("删除成功");
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        categoryService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 查询
     * @param categoryQueryDTO
     * @return
     */
    @PostMapping("/query")
    public Result<PageResult> query(@RequestBody CategoryQueryDTO categoryQueryDTO) {
        PageResult pageResult = categoryService.query(categoryQueryDTO);
        return Result.success(pageResult);
    }
    @GetMapping("/getAll")
    public Result<List<Category>> getAll() {
        List<Category> list = categoryService.list();
        return Result.success(list);
    }
}
